<template>
	<view class='page_cont'>
		<view class='top_wrap'>
			<view class='title_wrap'>
				<view class='title'>账号密码登录</view>
				<view class='subtitle'>未注册用户验证后自动登录</view>
			</view>
			<image class='logo' src="https://ossdev0.jiabala.com/5y6xEH8MGybeH"></image>
		</view>
		<view class='box'>
			<input class='input' maxlength="11" @input="input_mobile" placeholder="请输入账号" placeholder-style="color:rgba(66,71,82,0.5);"></input>
		</view>
		<view class='box' style="margin-bottom: 24rpx;">
			<input class='input' :password="pwd" maxlength="16" @input="input_password" placeholder="请输入密码" placeholder-style="color:rgba(66,71,82,0.5);"></input>
			<image class="icon" :src="pwd?'/static/icon60.png':'/static/icon59.png'" @click="pwd=!pwd"></image>
		</view>
		<view class='forget' @click="nav_set()">忘记密码</view>
		<view class='send_btn' :style="{background:mobile&&password?'rgba(91,144,255,1)':'rgba(91,144,255,0.5)'}" @click="user_login()">登录</view>
		<view class='agr_wrap' @click="change_agree()">
			<image class='icon' :src="agree?'/static/icon46.png':'/static/icon45.png'"></image>
			<view class='text'>同意家巴啦<text class='txt' @click.stop="nav_go(0)">《用户协议》</text>和<text class='txt' @click.stop="nav_go(1)">《隐私政策》</text></view>
		</view>
	</view>
</template>

<script>
	import config from "../../api/config.js"
	import util from "../../utils/util.js"
	import encryption from "../../utils/md5.js"
	import resume from "../../api/myResume.js"
	export default {
		data() {
			return {
				mobile: '',
				password: '',
				pwd: true,
				agree: true,
			}
		},
		methods: {
			//手机号
			input_mobile(e) {
				this.mobile = e.detail.value;
			},
			// 密码
			input_password(e) {
				this.password = e.detail.value;
			},
			// 忘记密码
			nav_set() {
				uni.navigateTo({
					url: '../forgetPassword/forgetPassword'
				})
			},
			// 同意
			change_agree(){
				this.agree=!this.agree;
			},
			// 协议
			nav_go(index){
			    uni.navigateTo({
					url: '../../mypage/webView/webView?index='+index
			    })
			},
			// 登录
			user_login() {
				if(!this.agree){
					util.page_show_toast("请先勾选同意家巴啦《用户协议》和《隐私政策》",1500)
					return false;
				}
				if(!util.testMobile(this.mobile)||this.mobile==''){
					util.page_show_toast('手机号码输入有误',1500)
					return false;
				}
				if(util.testWord(this.password)||this.password==''){
					util.page_show_toast('密码输入有误',1500)
					return false;
				}
				this.login_by_password();
			},
			// 登录
			login_by_password() {
				var params = {
					"phone": this.mobile,
					"password": encryption.md5(this.password)
				}
				config.login_by_password(params).then(res => {
					var info = res.data.data;
					// console.log(info)
					util.page_show_toast('登录成功', 1500);
					uni.setStorageSync('userinfo', info.userInfo)
					uni.setStorageSync('token', info.token)
					uni.setStorageSync('login_info', params)
					this.isFirStLogin()
				})
			},
			isFirStLogin(){
				config.isFirstLogin().then(res => {
					// console.log(res)
					if (res.data.code === '200') {
						if (res.data.data.accountProfile == null && res.data.data.accountBusinesses == null) {
							//
							this.get_org_list()
						} else if(res.data.data.accountProfile!==null && res.data.data.accountBusinesses == null){
							uni.setStorageSync("identityNum", 1)
							setTimeout(function() {
								uni.navigateBack({
									delta: 1,
									fail(){
										uni.switchTab({
											url: '/pages/index/index'
										})
									}
								})
							}, 1500)
							this.saveLoginStatus(res.data.data.accountProfile.id,2)
						}else if(res.data.data.accountProfile==null && res.data.data.accountBusinesses !== null){
							uni.setStorageSync("identityNum", 2)
							setTimeout(function() {
								uni.navigateBack({
									delta: 1,
									fail(){
										uni.switchTab({
											url: '/pages/index/index'
										})
									}
								})
							}, 1500)
							this.saveLoginStatus(res.data.data.accountBusinesses.id,1)
						}else if(res.data.data.accountProfile !== null && res.data.data.accountBusinesses !== null){
							if(res.data.data.accountProfile.isLogin===1){
								setTimeout(function() {
									uni.navigateBack({
										delta: 1,
										fail(){
											uni.switchTab({
												url: '/pages/index/index'
											})
										}
									})
								}, 1500)
								uni.setStorageSync("identityNum", 1)
								this.saveLoginStatus(res.data.data.accountProfile.id,2)
							}else{
								uni.setStorageSync("identityNum", 2)
								uni.setStorageSync("userIdentity",res.data.data.accountBusinesses)
								setTimeout(function() {
									uni.navigateBack({
										delta: 1,
										fail(){
											uni.switchTab({
												url: '/pages/index/index'
											})
										}
									})
								}, 1500)
								this.saveLoginStatus(res.data.data.accountBusinesses.id,1)
							}
						}
					}
				})
			},
			// 是否有认证机构
			get_org_list(){
				var params={
					"phone": uni.getStorageSync("userinfo").phone
				}
				config.get_org_list(params).then(res=>{
					var list=res.data.data;
					if(list&&list.length>0){
						var len=list.length;
						if(list[len-1].status==1){
							// 审核
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=3&status=2'
							})
						}else if(list[len-1].status==2){
							// 审核通过
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=2&status=2'+'&id='+list[len-1].businessId
							})
						}else if(list[len-1].status==4){
							// 审核未通过
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=1&status=2'+'&id='+list[len-1].businessId
							})
						}
					}else{
						uni.navigateTo({
							url: '../bindIdentity/bindIdentity'
						})
					}
				})
			},
			// 记录下次状态
			saveLoginStatus(id,identity){
				console.log(123456);
				resume.save_login_identity({id:id,identity:identity}).then(res=>{
					console.log(res);
				})
			}
		}
	}
</script>

<style lang="scss">
	.page_cont {
		padding: 64rpx 48rpx;

		.top_wrap {
			width: 100%;
			display: flex;
			flex-direction: row;
			margin-bottom: 172rpx;

			.title_wrap {
				flex: 1;

				.title {
					text-align: left;
					height: 74rpx;
					font-size: 52rpx;
					font-weight: 400;
					line-height: 74rpx;
					color: #3F4A58;
					margin-bottom: 4rpx;
				}

				.subtitle {
					text-align: left;
					height: 42rpx;
					font-size: 30rpx;
					font-weight: 400;
					line-height: 42rpx;
					color: #929BA7;
				}
			}

			.logo {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}
		}

		.box {
			padding: 32rpx 32rpx 32rpx 44rpx;
			background: #F7F9FB;
			border-radius: 16rpx;
			display: flex;
			flex-direction: row;
			margin-bottom: 32rpx;

			.input {
				flex: 1;
				text-align: left;
				min-height: 36rpx;
				height: 42rpx;
				font-size: 30rpx;
				font-weight: 400;
				line-height: 42rpx;
				color: rgba(66, 71, 82, 1);
				margin-right: 24rpx;
			}
			.icon{
				width: 48rpx;
				height: 48rpx;
			}
		}

		.forget {
			padding-right: 32rpx;
			text-align: right;
			height: 34rpx;
			font-size: 24rpx;
			font-weight: 400;
			line-height: 34rpx;
			color: #5B90FF;
		}

		.send_btn {
			margin: 32rpx 0;
			height: 104rpx;
			line-height: 104rpx;
			text-align: center;
			font-size: 36rpx;
			border-radius: 16rpx;
			font-weight: 600;
			color: #fff;
			letter-spacing: 4rpx;
		}

		.agr_wrap {
			margin: 0 90rpx;
			display: flex;
			flex-direction: row;
			align-items: center;

			.icon {
				width: 28rpx;
				height: 28rpx;
				margin-right: 12rpx;
				margin-top: 2rpx;
			}

			.text {
				flex: 1;
				height: 34rpx;
				font-size: 24rpx;
				font-weight: 400;
				line-height: 34rpx;
				color: rgba(146, 155, 167, 0.5);

				.txt {
					color: #929BA7;
				}
			}
		}
	}
</style>
